package com.twu.biblioteca;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;


/**
 * Created with IntelliJ IDEA.
 * User: Deepika
 * Date: 5/13/13
 * Time: 6:53 PM
 * To change this template use File | Settings | File Templates.
 */

import static javax.swing.JOptionPane.showMessageDialog;

class BOOK {
    private String name;
    private int available;            // 1 indicates book is available, 0 indicates not available

    BOOK(String name, int available){
        this.name = name;
        this.available = available;
    }

    BOOK(String name){
        this.name = name;
        this.available = 1;
    }

    public String get_name(){
        return name;
    }

    public int check_availability(){
        return available;
    }

    public void set_availability(int available){
        this.available = available;
    }
}


class Movies {
    private String name;
    private Integer year;
    private String director;
    private String rating;

    Movies(String name, Integer year, String director, String rating) {
        this.name = name;
        this.year = year;
        this.director = director;
        this.rating = rating;
    }

    public String get_name(){
        return name;
    }

    public Integer get_year(){
        return year;
    }

    public String get_director(){
        return director;
    }

    public String get_rating(){
        return rating;
    }
}



public class Biblioteca{
    private JPanel panel1;
    private JToolBar ToolBar;
    private JButton viewBooksInLibraryButton;
    private JButton reserveABookButton;

    private JButton reserveButton;
    private JButton membershipDetailsButton;
    ArrayList<BOOK> books = new ArrayList<BOOK>();
    ArrayList<Movies> Movies_list = new ArrayList<Movies>();
    private JButton viewMoviesButton;
    private JTable table1;


    public void initialise_books() {
        BOOK b1 = new BOOK("Gone with the Wind",0);
        BOOK b2 = new BOOK("Sherlock Homes",1);
        books.add(b1);
        books.add(b2);
    }

    public void initialise_movies() {
        Movies M1 = new Movies("Sholay",1990,"Ramesh Sippy","1");
        Movies M2 = new Movies("Ashiqui 2",2013,"Director","N/A");
        Movies_list.add(M1);
        Movies_list.add(M2);
    }

    public void add_books_to_table() {

        DefaultTableModel tableModel = new DefaultTableModel(0,0);
        String header[] = new String[]{"Books"};
        tableModel.setColumnIdentifiers(header);
        table1.setModel(tableModel);

        for(BOOK b: books){
            Vector v = new Vector();
            v.add(b.get_name());
            tableModel.addRow(v);
          }

            table1.setVisible(true);
    }

    public void add_movies_to_table() {
        DefaultTableModel tableModel = new DefaultTableModel(0,0);
        String header[] = new String[]{"Movie","Year","Director","Rating"};
        tableModel.setColumnIdentifiers(header);
        table1.setModel(tableModel);

        for(Movies m: Movies_list){
            Vector v = new Vector();
            v.add(m.get_name());
            v.add(m.get_year());
            v.add(m.get_director());
            v.add(m.get_rating());
            tableModel.addRow(v);
        }

        table1.setVisible(true);
    }


    public Biblioteca() {

        //do not display the reserve button initially
        reserveButton.setVisible(false);
        table1.setVisible(false);


        initialise_books();
        initialise_movies();



        viewBooksInLibraryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                add_books_to_table();
                table1.setEnabled(false);
                reserveButton.setVisible(false);
            }
        });


        reserveABookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                add_books_to_table();
                table1.setEnabled(true);
                reserveButton.setVisible(true);
            }
        });


        membershipDetailsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showMessageDialog(null, "Please talk to a Librarian. Thank you.");
            }
        });

        reserveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = table1.getSelectedRowCount();
                //showMessageDialog(null, list1.getSelectedValue());
                BOOK b = books.get(index);
                if(b.check_availability() == 0){
                    showMessageDialog(null, "Sorry we don't have that book yet.");
                }
                else  {
                    showMessageDialog(null, "Thank You! Enjoy the book.");
                    b.set_availability(0);
                }
            }
        });


        viewMoviesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                add_movies_to_table();
                table1.setEnabled(false);
                reserveButton.setVisible(false);
            }
        });
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("Welcome to Biblioteca");
        frame.setSize(500,500);
        frame.setLocation(300,300);
        frame.setContentPane(new Biblioteca().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
        showMessageDialog(null, "Welcome to Biblioteca!");
    }



}
